package cn.itcast.bassm.util;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpRequest;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import cn.itcast.bassm.pojo.StatisticsSnapshot;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class CreateSimpleExcelToDisk {
	  /** 
     * @功能：手工构建一个简单格式的Excel 
     */  
    private static List<StatisticsSnapshot> getStudent(int naber) throws Exception  
    {  
        List list = new ArrayList(); 
        StatisticsSnapshot user1 = new StatisticsSnapshot(naber);
        list.add(user1);  
        return list;  
    }  
  
    public static String ConExport(JsonArray array, String tedaytime,String typexls) throws Exception  
    {  
        // 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("统计");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  
        HSSFCell cell = row.createCell((short) 0);  
        cell.setCellValue("年龄");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 1);  
        cell.setCellValue("姓名");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 2);  
        cell.setCellValue("相机名称");  
        cell.setCellStyle(style);  
        cell = row.createCell((short) 3);  
        cell.setCellValue("人脸库名");  
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);  
        cell.setCellValue("创建时间");  
        cell.setCellStyle(style);  
 
  
        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，  
        //List list = CreateSimpleExcelToDisk.getStudent(naber);  
        String json = null;
		String age = null;
		String personName= null;
		String cameraName=null;
		String dbName=null;
        for (int i = 0; i < array.size(); i++)   {  
            row = sheet.createRow((int) i + 1);  
            JsonObject subObject = array.get(i).getAsJsonObject();
            JsonObject subObject2 = subObject.get("person").getAsJsonObject();
            //long createDate=subObject2.get("createDate").getAsLong();
            String createDate=subObject.get("createDate").getAsString();
            // 第四步，创建单元格，并设置值  
            row.createCell((short) 0).setCellValue((String ) subObject.get("age").getAsString());
            row.createCell((short) 1).setCellValue((String ) subObject.get("personName").getAsString()); 
            row.createCell((short) 2).setCellValue((String ) subObject.get("cameraName").getAsString());
            row.createCell((short) 3).setCellValue((String ) subObject.get("dbName").getAsString());
            row.createCell((short) 4).setCellValue((String ) transferLongToDate(createDate));
        }  
        // 第六步，将文件存到指定位置  
        try  
        {  
            FileOutputStream fout = new FileOutputStream("/home/apache-tomcat-8.0.24/webapps/xls/"+tedaytime+typexls+".xls");
            wb.write(fout);  
            fout.close();  
            String urlxls=tedaytime+typexls;
            String msg2= MailUtil.SenMailGo(urlxls);
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }
        String msg3="成功";
		return msg3;
    }  
    
 
    /**
     * long 转换date
     * @param dateFormat
     * @param millSec
     * @return
     */
    private  static String transferLongToDate(String createDate){
    	long createDatelong = Long.valueOf(createDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        java.util.Date date = new Date(createDatelong);  
        String str = sdf.format(date);  
               return str;

       }
}  

